#include "function.h"
//#include "weather.h"
//#include "calender.h"
/***********declear of function*************/
LV_FONT_DECLARE(siyuanrouti_20);
void Function_Init(lv_obj_t *Function);
/********************************************/
static unsigned char Creating_page_flag=0;//1  the task is creating page
lv_obj_t *meter=NULL;
lv_obj_t *tabview=NULL;
lv_meter_indicator_t * indic_hour;
lv_meter_indicator_t * indic_min;
lv_meter_indicator_t * indic_sec;
unsigned char sec_cnt=0;
unsigned char min_cnt=0;
unsigned char hour_cnt=0;
unsigned char Real_hour=0;
lv_obj_t *Clock=NULL;
lv_obj_t *Function=NULL;
lv_timer_t * Clock_Update=NULL;
lv_timer_t * del_out_page=NULL;
static lv_style_t style_page;//the style of the new page
lv_obj_t*new_page;
lv_anim_t a;
unsigned char Flag_new_page=0;
lv_group_t * g;
extern lv_indev_t * indev_keypad;
/*******************************************/


//to initialize the new page style
static void Page_style_Init(void){
	lv_style_init(&style_page);
	lv_style_set_size(&style_page, 240);
	lv_style_set_radius(&style_page,240);
	lv_style_set_align(&style_page,LV_ALIGN_CENTER);
}


//to delete the function page after the page change animation is end
void Out_Function_page_del(lv_timer_t * timer){
	//delete the function page to free internal storage
	lv_obj_del(Function);
	//Reset the Creating_page bit
	Creating_page_flag=0;
}


/***********Page Change Animation*********/
//the implement of the create new page animation
static void set_create_page_anim(void * var, int32_t v)
{
	lv_obj_set_height((lv_obj_t *)var, v);
}


//Initialize the page animation
static void Page_anim_Init(void){
	lv_anim_init(&a);
	lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t) set_create_page_anim);

	/*Length of the animation [ms]*/
	lv_anim_set_time(&a, 100);

	/*Set start and end values. E.g. 0, 150*/
	lv_anim_set_values(&a, 0, 240);
}



/***********Page Change Function *********/
//switch from the clock page to the function selecting page
static void Clock_to_Function(lv_event_t*e){
	//to create the function page and initialize the function page
	Function = lv_tabview_add_tab(tabview, "");
	lv_obj_set_scrollbar_mode(Function, LV_SCROLLBAR_MODE_OFF);
    Function_Init(Function);
	//移除时钟按钮选中功能
    lv_group_remove_obj(meter);
	lv_indev_set_group(indev_keypad, g);
	lv_tabview_set_act(tabview, 1, LV_ANIM_OFF);
}

//switch from the function selecting page to the  clock  page
static void Function_to_Clock(lv_event_t*e){
    //delete the function page to free internal storage
	lv_obj_del(Function);
	lv_group_add_obj(g,meter);
	lv_indev_set_group(indev_keypad, g);
	//select the Clock page
	lv_tabview_set_act(tabview, 0, LV_ANIM_OFF);
}

//switch from the function selecting page to the  weather  page
static void Function_to_Weather(lv_event_t*e){
	//if this time another task is creating task,forbit this page create function
	if(Creating_page_flag==0){
	Creating_page_flag=1;
	new_page=lv_obj_create(lv_scr_act());
	lv_obj_add_style(new_page,&style_page,NULL);
    lv_obj_set_scrollbar_mode(new_page, LV_SCROLLBAR_MODE_OFF);//forbid the scroll function of the backgound
	Weather_Init(new_page);//create the weather page
	/*Set the "animator" function*/
	lv_anim_set_var(&a, new_page);
	lv_anim_start(&a);
	//delete the function selecting page 1000ms later to free the storage
	Clock_Update=lv_timer_create(Out_Function_page_del, 500, NULL);
	lv_timer_set_repeat_count(Clock_Update,1);

	}
}


//switch from the function selecting page to the  setting  page
static void Function_to_Setting(lv_event_t*e){
	if(Creating_page_flag==0){
	Creating_page_flag=1;
	new_page=lv_obj_create(lv_scr_act());
	lv_obj_add_style(new_page,&style_page,NULL);
	Setting_Init(new_page);
	/*Set the "animator" function*/
	lv_anim_set_var(&a, new_page);
	lv_anim_start(&a);
	Clock_Update=lv_timer_create(Out_Function_page_del, 500, NULL);
	lv_timer_set_repeat_count(Clock_Update,1);
	}
}


//switch from the function selecting page to the  calender  page
static void Function_to_Calendar(lv_event_t*e){
	if(Creating_page_flag==0){
	Creating_page_flag=1;
	new_page=lv_obj_create(lv_scr_act());
	lv_obj_add_style(new_page,&style_page,NULL);
	lv_obj_set_scrollbar_mode(new_page, LV_SCROLLBAR_MODE_OFF);//forbid the scroll function of the backgound
	Calender_Init(new_page);//create the calender
	/*Set the "animator" function*/
	lv_anim_set_var(&a, new_page);
	lv_anim_start(&a);

	Clock_Update=lv_timer_create(Out_Function_page_del, 500, NULL);
	lv_timer_set_repeat_count(Clock_Update,1);
	}
}

//switch from the function selecting page to the  game  page
static void Function_to_Game(lv_event_t*e){
	if(Creating_page_flag==0){
	Creating_page_flag=1;
	new_page=lv_obj_create(lv_scr_act());
	lv_obj_add_style(new_page,&style_page,NULL);
	lv_obj_set_scrollbar_mode(new_page, LV_SCROLLBAR_MODE_OFF);//forbid the scroll function of the backgound
    Game_Init(new_page);
	/*Set the "animator" function*/ 
	lv_anim_set_var(&a, new_page);
	lv_anim_start(&a);
	Clock_Update=lv_timer_create(Out_Function_page_del, 500, NULL);
	lv_timer_set_repeat_count(Clock_Update,1);
    }
}

lv_obj_t *label_weather_fun;//as the function remain label
lv_obj_t *label_clock;//as the function remain label
lv_obj_t *label_calender;//as the function remain label
lv_obj_t *label_setting;//as the function remain label
lv_obj_t *label_game;//as the function remain label
lv_obj_t * obj_function;//功能选项背景
extern uint8_t  yuyan_flag;    
extern uint8_t back_flag;//0 关闭  1开启
/***************Init the Function page**************/
void Function_Init(lv_obj_t *Function){
	static lv_style_t style_btn;
	static lv_style_t style_btn_fous;
	static lv_style_t style_label;
	lv_obj_t * btn;
	//create one black circular as the background
	obj_function=lv_obj_create(Function);
	lv_obj_center(obj_function);
	lv_obj_set_size(obj_function,245,245);
	if(back_flag)lv_obj_set_style_bg_color(obj_function,lv_color_hex(0x000000),NULL);
	else       lv_obj_set_style_bg_color(obj_function,lv_color_hex(0xA2CE59),NULL);
	lv_obj_set_style_radius(obj_function,240,NULL);

	//to initialize one style to decorate the btn
	lv_style_init(&style_btn);
	lv_style_set_bg_color(&style_btn, lv_color_hex(0xF46B43));
	lv_style_set_width(&style_btn,90);
	lv_style_set_height(&style_btn,45);
	lv_style_set_radius(&style_btn,10);

	//to initialize one style to decorate the btn when foucus
	lv_style_init(&style_btn_fous);
	lv_style_set_transform_width(&style_btn_fous,5);
	lv_style_set_transform_height(&style_btn_fous,-5);
	
	
	//to initialize one style to decorate the word
	lv_style_init(&style_label);
	lv_style_set_text_font(&style_label,&siyuanrouti_20);
	lv_style_set_text_color(&style_label,lv_color_hex(0xF0F0F0));
	lv_style_set_align(&style_label,LV_ALIGN_CENTER);

	//create the weather select btn
	btn=lv_btn_create(obj_function);
	lv_obj_align(btn,LV_ALIGN_TOP_MID,5,-15);
	lv_obj_add_style(btn,&style_btn,NULL);
	lv_obj_add_style(btn,&style_btn_fous,LV_STATE_FOCUSED);
	label_weather_fun=lv_label_create(btn);
	lv_obj_add_style(label_weather_fun,&style_label,NULL);
	if(yuyan_flag) lv_label_set_text(label_weather_fun,"Weather");
	else    lv_label_set_text(label_weather_fun,"天气");
	lv_obj_add_event_cb(btn,Function_to_Weather,LV_EVENT_CLICKED,NULL);

	//create the Clock select btn
	btn=lv_btn_create(obj_function);
	lv_obj_add_style(btn,&style_btn,NULL);
	lv_obj_add_style(btn,&style_btn_fous,LV_STATE_FOCUSED);
	lv_obj_set_style_bg_color(btn,lv_color_hex(0x42B857),NULL);
	lv_obj_align(btn,LV_ALIGN_CENTER,-10,-7);
	label_clock=lv_label_create(btn);
	lv_obj_add_style(label_clock,&style_label,NULL);
	if(yuyan_flag) lv_label_set_text(label_clock,"Clock");
	else    lv_label_set_text(label_clock,"时钟");
	//Clock switch cb_task
	lv_obj_add_event_cb(btn,Function_to_Clock,LV_EVENT_CLICKED,NULL);

	//create the Setting select btn
	btn=lv_btn_create(obj_function);
	lv_obj_add_style(btn,&style_btn,NULL);
	lv_obj_add_style(btn,&style_btn_fous,LV_STATE_FOCUSED);
	lv_obj_set_style_bg_color(btn,lv_color_hex(0x0AB99B),NULL);
	lv_obj_align(btn,LV_ALIGN_CENTER,50,30);
	label_setting=lv_label_create(btn);
	lv_obj_add_style(label_setting,&style_label,NULL);
	if(yuyan_flag) lv_label_set_text(label_setting,"Setting");
	else    lv_label_set_text(label_setting,"设置");
	//Setting switch cb_task
	lv_obj_add_event_cb(btn,Function_to_Setting,LV_EVENT_CLICKED,NULL);

	//create the Calender select btn
	btn=lv_btn_create(obj_function);
	lv_obj_add_style(btn,&style_btn,NULL);
	lv_obj_add_style(btn,&style_btn_fous,LV_STATE_FOCUSED);
	lv_obj_set_style_bg_color(btn,lv_color_hex(0xA2CE59),NULL);
	lv_obj_align(btn,LV_ALIGN_CENTER,-60,40);
	label_calender=lv_label_create(btn);
	lv_obj_add_style(label_calender,&style_label,NULL);
	if(yuyan_flag) lv_label_set_text(label_calender,"Calender");
	else lv_label_set_text(label_calender,"日历");
	//Calender switch cb_task
	lv_obj_add_event_cb(btn,Function_to_Calendar,LV_EVENT_CLICKED,NULL);

    //create the Game select btn
	btn=lv_btn_create(obj_function);
	lv_obj_add_style(btn,&style_btn,NULL);
	lv_obj_add_style(btn,&style_btn_fous,LV_STATE_FOCUSED);
	lv_obj_set_style_bg_color(btn,lv_color_hex(0xF387B6),NULL);
	lv_obj_align(btn,LV_ALIGN_CENTER,-10,70);
	label_game=lv_label_create(btn);
	lv_obj_add_style(label_game,&style_label,NULL);
	if(yuyan_flag) lv_label_set_text(label_game,"photos");
	else             lv_label_set_text(label_game,"相册");
	//Game switch cb_task
	lv_obj_add_event_cb(btn,Function_to_Game,LV_EVENT_CLICKED,NULL);

}



/**********************Clock_Start********************************/
//change the Real hour number to hour_cnt number
 void Real_hour_to_hout_cnt(void){
if(Real_hour<12){
	hour_cnt=Real_hour*5+min_cnt/12;
}
else if(Real_hour>=12){
	hour_cnt=(Real_hour-12)*5+min_cnt/12;
}
}

extern struct tm timeinfo;
//Clock Update Task,this task will execute per 300ms
void Update_task(lv_timer_t * timer){
/* 	sec_cnt++;
	lv_meter_set_indicator_value(meter,indic_sec,sec_cnt);//update the sec
	if(sec_cnt==60){
		sec_cnt%=60;
		min_cnt++;
		lv_meter_set_indicator_value(meter,indic_min,min_cnt);//update the min
		if(min_cnt%12==0){
			min_cnt%=60;
			hour_cnt++;
			lv_meter_set_indicator_value(meter,indic_hour,hour_cnt);//update the hour
			if(hour_cnt==60) hour_cnt%=60;
		}
	} */
	 if (!getLocalTime(&timeinfo))
    {
        return ;
    }
    min_cnt=timeinfo.tm_min;
    Real_hour=timeinfo.tm_hour;
    Real_hour_to_hout_cnt();
    sec_cnt=timeinfo.tm_sec;
	lv_meter_set_indicator_value(meter,indic_sec,sec_cnt);
	lv_meter_set_indicator_value(meter,indic_min,min_cnt);
	lv_meter_set_indicator_value(meter,indic_hour,hour_cnt);
}

extern uint8_t back_flag;
void Clock_Init(lv_obj_t *Clock){
		//to create one clock dial
	    meter = lv_meter_create(Clock);
		lv_obj_set_size(meter,245, 245);
		if(back_flag) lv_obj_set_style_bg_color(meter,lv_color_hex(0x0),LV_PART_MAIN );
		else          lv_obj_set_style_bg_color(meter,lv_color_hex(0xA2CE59),LV_PART_MAIN );
		lv_obj_center(meter);

		//add my logo
		lv_obj_t*my_logo=lv_label_create(meter);
		lv_label_set_text(my_logo,"sense_long");
		lv_label_set_recolor(my_logo,true);
		lv_label_set_text(my_logo,"#29814A sens##AAA850 e_long#");
		lv_obj_align(my_logo,LV_ALIGN_CENTER,0,-30);
		
		lv_obj_add_event_cb(meter,Clock_to_Function,LV_EVENT_CLICKED,NULL);
		/*Create a scale for the minutes*/
		/*61 ticks in a 360 degrees range (the last and the first line overlaps)*/
		lv_meter_scale_t * scale_min = lv_meter_add_scale(meter);
		lv_meter_set_scale_ticks(meter, scale_min, 61, 0, 0, lv_palette_main(LV_PALETTE_GREY));
		lv_meter_set_scale_range(meter, scale_min, 0, 60, 360, 270);

		/*Create another scale for the hours. It's only visual and contains only major ticks*/
		lv_meter_scale_t * scale_hour = lv_meter_add_scale(meter);
		lv_meter_set_scale_ticks(meter, scale_hour, 12, 0, 0, lv_color_hex(0xFFFFFF));			   /*12 ticks*/
		lv_meter_set_scale_major_ticks(meter, scale_hour, 1, 2, 20, lv_color_hex(0xFFFFFF), 10);	  /*Every tick is major*/
		lv_meter_set_scale_range(meter, scale_hour, 1, 12, 330, 300);

		//create three needle for the min銆乭our銆乻ec
		indic_min = lv_meter_add_needle_line(meter, scale_min, 4, lv_palette_main(LV_PALETTE_GREEN), -30);
		indic_hour = lv_meter_add_needle_line(meter, scale_min,6, lv_palette_main(LV_PALETTE_RED), -50);
		indic_sec = lv_meter_add_needle_line(meter, scale_min, 2, lv_palette_main(LV_PALETTE_BLUE), -15);
		//Initialize the time by wifi
		lv_meter_set_indicator_value(meter,indic_sec,sec_cnt);
		lv_meter_set_indicator_value(meter,indic_min,min_cnt);
		lv_meter_set_indicator_value(meter,indic_hour,hour_cnt);
		//create the cloke update task,execute this task one time per second
		Clock_Update=lv_timer_create(Update_task, 300, NULL);

}

/********************Clock_end*********************************/

 void Project_Init(void){
	Page_anim_Init();//This function may be put under ever page_create task
	Page_style_Init();//initialize the new page style
	tabview= lv_tabview_create(lv_scr_act(), LV_DIR_BOTTOM, 0);
	lv_obj_set_size(tabview,240,240);
	lv_obj_set_style_radius(tabview,240,NULL);
	lv_obj_center(tabview);

	Clock = lv_tabview_add_tab(tabview, "");
	lv_obj_set_scrollbar_mode(Clock, LV_SCROLLBAR_MODE_OFF);
    Clock_Init(Clock);

	g = lv_group_create();
	lv_group_set_default(g);
	lv_group_add_obj(g,meter);
	lv_indev_set_group(indev_keypad, g);
}
